<?php
/**
 * Object Relational Persistence Mapping Class for table: lab_queue
 * @package modules.dept_laboratory.ordo
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class LabQueue extends ORDataObject {

	/**#@+
	 * Fields of table: lab_queue mapped to class members
	 */
	var $lab_queue_id		= '';
	var $start_waiting		= '';
	var $start_process = '';
	var $lab_assignment_ticket_id		= '';
	/**#@-*/


	/**
	 * DB Table
	 */
	var $_table = 'lab_queue';

	/**
	 * Primary Key
	 */
	var $_key = 'lab_queue_id';
	
	/**
	 * Internal Name
	 */
	var $_internalName = 'LabQueue';

	/**
	 * Handle instantiation
	 */
	function __construct() {
		parent::ORDataObject();
	}
	
	/**
     * Delete all items of a givent labTicketId.
     * @param $labTicketId
     * @return labTicketId
     */
    public function deleleQueueItem($labAssignmentTicketId) {
        $sql = "DELETE FROM $this->_table WHERE lab_assignment_ticket_id='$labAssignmentTicketId'";
        $this->_execute($sql);
        return $labAssignmentTicketId;      
    }
	
    public function setData($labQueueInfo) {
       if (is_array($labQueueInfo)) {
            $this->populateArray($labQueueInfo);
        }
    }
    public function updateStartProcess($labAssignmentTicketId) {
    	$sql = "UPDATE $this->_table SET start_process = NOW() 
    	WHERE lab_assignment_ticket_id = '$labAssignmentTicketId'";
        $this->_execute($sql);       
    }
    public function isItemExist($labAssignmentTicketId) {
    	$sql = "SELECT lab_assignment_ticket_id from $this->_table 
    	       WHERE lab_assignment_ticket_id ='$labAssignmentTicketId'";
        $res = $this->_Execute($sql);
        if ($res && !$res->EOF)
        {
            return true;
        }
        return false;
    	
    }
    
    public function listQueueItems($patientType) {
    	$field =   L10nHelper::changeField(L10nHelper::getLocale(), 'name');        
        
        $patientType = Utils::getEnumKey(EnumerationConstant::PATIENT_TYPE,
        $patientType);
        $ds = & new Datasource_sql();
        $sql = array(
        'cols'=>"lab_queue_id, t.lab_assignment_ticket_id, lat.patient_id, p.name patient_name, lat.enum_patient_type,
                start_waiting, start_process, treatment_department_id, dept.$field treatment_department_name, 
                lat.enum_status, lg.$field lab_group_name, lt.is_done, lt.lab_ticket_id",
        'from'=>"$this->_table t 
                INNER JOIN lab_assignment_ticket lat ON (t.lab_assignment_ticket_id=lat.lab_assignment_ticket_id AND lat.enum_patient_type='$patientType')
                INNER JOIN lab_ticket lt ON (t.lab_assignment_ticket_id=lt.lab_assignment_ticket_id)
                INNER JOIN person p ON (lat.patient_id=p.person_id)
                INNER JOIN lab_group lg ON (lt.lab_group_id=lg.lab_group_id)
                LEFT JOIN department dept ON (lat.treatment_department_id=dept.department_id)                
                "
        );
        
        $ds = & new Datasource_sql();
        $ds->setup($this->_db, $sql);
        $result = $ds->toArray(); 
              
        return $result;
    }
    
    public function deleteItem($labAssignmentTicketId) {
    	$sql = "DELETE FROM $this->_table WHERE lab_assignment_ticket_id='$labAssignmentTicketId'";
    	$this->_execute($sql);
    }

	

}
?>
